package org.framework.common.ext.util;

import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.framework.common.ext.util.DateUtil;
import org.framework.common.ext.util.PropertiesUtil;
import org.framework.common.ext.util.StringUtil;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {

	private static final String TITLE_CHECKED = "签到提醒";
	private static final String TITLE_MESSAGE = "蜜娃娃公告";
	
	private static boolean APNS   = false;
	private static String APPKEY  = "0c19afc3f91f8211fc952f89";
	private static String SECRET  = "40cf57a1bea85c6cf4fe8edb";
	private static String MESSAGE = "亲爱的家长，您的宝宝今日有课，别忘了签到哦！";
	
	// 注册配置文件
	static { registConfig(); }
		
	public static void sendPush(final boolean alias, final String message, final String[] senders) {
        try {
        	String content = StringUtil.isEmpty(message) ? MESSAGE : message;
        	
        	// 构建JPushClient
            JPushClient jpushClient = new JPushClient(SECRET, APPKEY);
            
            // 创建jpush构建器
            PushPayload.Builder builder = PushPayload.newBuilder();
            
            // 设置jpush参数
            builder.setPlatform(Platform.android_ios());	
            builder.setAudience(alias ? Audience.alias(senders) : Audience.tag(senders));	
    		builder.setNotification(Notification.newBuilder().addPlatformNotification(android(alias, content)).addPlatformNotification(ios(alias, content)).build());

    		// 构建PushPayload对象
            PushPayload payload = builder.build();
            
            // 设置推送环境(false=开发环境、true=生产环境)
            payload.resetOptionsApnsProduction(APNS);
            
			// 调用推送接口
            PushResult result = jpushClient.sendPush(payload);
            
            System.out.println("推送[jpush]签到通知："+result.getResponseCode()+" ["+DateFormatUtils.format(new Date(), DateUtil.DatePattern.DATETIME.value())+"]");
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
        	e.printStackTrace();
        }
	}
	
	/*
	 * 构造android通知参数
	 */
	private static AndroidNotification android(final boolean alias, final String message){
		return AndroidNotification.newBuilder()
				.setAlert(message)
				.setTitle(alias ? TITLE_CHECKED : TITLE_MESSAGE)
				.addExtra("msg_type", alias ? "0" : "1")
				.build();
	}
	
	/*
	 * 构造IOS通知参数
	 */
	private static IosNotification ios(final boolean alias, final String message){
		return IosNotification.newBuilder()
				.setAlert(message)
				.setBadge(1)
				.addExtra("msg_type", alias ? "0" : "1")
				.build();
	}
	
	/*
	 * 注册配置文件
	 */
	private static void registConfig(){
		PropertiesUtil properties = PropertiesUtil.loader("/config/properties/config.properties");
		APNS = Boolean.valueOf(properties.getProperty("jpush.config.apns"));
		APPKEY = properties.getProperty("jpush.config.appkey");
		SECRET = properties.getProperty("jpush.config.secret");
		MESSAGE = properties.getProperty("jpush.config.message");
	}
	
	public static void main(String[] args) {
		sendPush(false, "公告推送测试", new String[]{"297ebe0e53e99be90153eab6e9b90021"});
	}
}
